package concurrent.threadSafe;

import java.util.concurrent.Semaphore;

/**
 * @author 刘正星
 * @version 1.0.0
 * @ClassName OrderThread.java
 * @Description 三个线程交替执行
 * @createTime 2021年04月14日 00:20:00
 */
public class OrderThread {
    //利用信号量来限制

    private static Semaphore s1 = new   Semaphore(1);
    private static Semaphore s2 = new   Semaphore(1);
    private static Semaphore s3 = new   Semaphore(1);

    public static void main(String[] args) {
        try {
            s1.acquire();
            s2.acquire();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(()->{
            while (true){
                try {
                    s1.acquire();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("A");
                s2.release();
            }
        }).start();

        new Thread(()->{
            while (true){
                try {
                    s2.acquire();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("B");
                s3.release();
            }
        }).start();

        new Thread(()->{
            while (true){
                try {
                    s3.acquire();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("C");
                s1.release();
            }
        }).start();



    }
}
